package com.renwei.yunlong.activity.work;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.news.ProblemAssignActivity;
import com.renwei.yunlong.adapter.TablayoutAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.PowerBean;
import com.renwei.yunlong.event.ComplainPageRefreshEvent;
import com.renwei.yunlong.fragment.BaseHistoryFragment;
import com.renwei.yunlong.fragment.ComplainResultFragment;
import com.renwei.yunlong.fragment.WorkDetailsFragment;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import github.opensource.dialog.prompt.PromptButton;
import github.opensource.dialog.prompt.PromptButtonListener;
import github.opensource.dialog.prompt.PromptDialog;

/**
 * 投诉信息---页面
 * <p>
 * Created by Administrator on 2017/8/28.
 */

public class ComplaintsInformationActivity extends BaseActivity implements View.OnClickListener, PromptButtonListener {

    @Bind(R.id.tab_layout)
    TabLayout mTabLayout;
    @Bind(R.id.v_pager)
    ViewPager mViewPager;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.btn_next)
    Button btnNext;
    @Bind(R.id.rl_button)
    RelativeLayout rlButton;

    private TablayoutAdapter adapter;

    private String requestId;

    private Intent intent;

    private static final String POWER_QUEREN = "确认责任人";
    private static final String POWER_FANKUI = "反馈";
    private static final String POWER_ZHIPAIGEI = "指派给";
    private static final String POWER_CHULI = "建议";
    private HashSet<String> powerSet;
    private PromptDialog promptDialog;
    private String outsourceFlag;
    private String newRequestId;
    private String complainId;
    private String sendUserName;
    private String newServerRequestId;
    private String serverRequestId;
    private PowerBean bean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaints_information);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        EventBus.getDefault().register(this);

        requestId = getIntent().getStringExtra("requestId");
        newRequestId = getIntent().getStringExtra("newRequestId");
        serverRequestId = getIntent().getStringExtra("serverRequestId");
        newServerRequestId = getIntent().getStringExtra("newServerRequestId");
        outsourceFlag = getIntent().getStringExtra("outsourceFlag");
        complainId = getIntent().getStringExtra("complainId");
        sendUserName = getIntent().getStringExtra("sendUserName");
        initView();
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onStateRefresh(ComplainPageRefreshEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("投诉信息");
        ivBack.setOnClickListener(this);
        btnNext.setOnClickListener(this);

        ArrayList<Fragment> list = new ArrayList<>();
        if ("1".equals(companyType) && "1".equals(outsourceFlag)) {  // 业主 外
            list.add(new ComplainResultFragment(newServerRequestId, outsourceFlag));
            list.add(new BaseHistoryFragment(newServerRequestId, "complain", outsourceFlag));
            list.add(new WorkDetailsFragment(serverRequestId, outsourceFlag, false));
        } else {
            list.add(new ComplainResultFragment(newRequestId, outsourceFlag));
            list.add(new BaseHistoryFragment(newRequestId, "complain", outsourceFlag));
            list.add(new WorkDetailsFragment(requestId, outsourceFlag, false));
        }
        String[] titles = {"反馈结果", "处理过程", "工单详情"};
        //ViewPager的适配器
        adapter = new TablayoutAdapter(getSupportFragmentManager(), list, titles);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(3);
        //绑定
        mTabLayout.setupWithViewPager(mViewPager);
        // 弹框
        promptDialog = new PromptDialog(this);
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", complainId);
        map.put("type", "1");
        ServiceRequestManager.getManager().getProbCompState(this, JsonMapUtil.mapToJson(map), new HttpTaskListener() {
            @Override
            public void onSuccess(int requestId, String result) {
                bean = new Gson().fromJson(result, PowerBean.class);
                powerSet = new HashSet<String>();
                if ("1".equals(companyType)) {
                    //业主投诉判断
                    if ("0".equals(bean.getRows().getOutsourceFlag())) {
                        if ("12".equals(ownerBean.getRows().getEmployee().getRoleId()) || "10".equals(ownerBean.getRows().getEmployee().getRoleId())) {
                            powerSet.add(POWER_QUEREN);
                            if ("-1".equals(bean.getRows().getNowStat()) && "0".equals(bean.getRows().getComplainFlag())) {
                                //反馈  指派给
                                powerSet.add(POWER_FANKUI);
                                powerSet.add(POWER_ZHIPAIGEI);
                            } else if ((("0".equals(bean.getRows().getNowStat()) && "1".equals(bean.getRows().getComplainFlag()))
                                    || ("3".equals(bean.getRows().getNowStat()) && "3".equals(bean.getRows().getComplainFlag())))
                                    && bean.getRows().getResponseUserId().equals(ownerBean.getRows().getEmployeeId())) {
                                //反馈  指派给
                                powerSet.add(POWER_FANKUI);
                                powerSet.add(POWER_ZHIPAIGEI);
                            }
                        } else {
                            if ("0".equals(bean.getRows().getNowStat()) && "1".equals(bean.getRows().getComplainFlag())
                                    && bean.getRows().getResponseUserId().equals(ownerBean.getRows().getEmployeeId())) {
                                //处理   指派给
                                powerSet.add(POWER_CHULI);
                                powerSet.add(POWER_ZHIPAIGEI);
                            }
                        }
                    }
                } else {
                    if ("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId())) {
                        powerSet.add(POWER_QUEREN);
                        if ("-1".equals(bean.getRows().getNowStat()) && "0".equals(bean.getRows().getComplainFlag())) {
                            //反馈  指派给
                            powerSet.add(POWER_FANKUI);
                            powerSet.add(POWER_ZHIPAIGEI);
                        } else if ((("0".equals(bean.getRows().getNowStat()) && "1".equals(bean.getRows().getComplainFlag()))
                                || ("3".equals(bean.getRows().getNowStat()) && "3".equals(bean.getRows().getComplainFlag())))
                                && bean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId())) {
                            //反馈  指派给
                            powerSet.add(POWER_FANKUI);
                            powerSet.add(POWER_ZHIPAIGEI);
                        }
                    } else {
                        if ("0".equals(bean.getRows().getNowStat()) && "1".equals(bean.getRows().getComplainFlag())
                                && bean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId())) {
                            //处理   指派给
                            powerSet.add(POWER_CHULI);
                            powerSet.add(POWER_ZHIPAIGEI);
                        }
                    }
                }
                if (powerSet.size() == 0) {
                    rlButton.setVisibility(View.GONE);
                } else {
                    rlButton.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onException(int requestId, String code) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.btn_next:
                setDialog();
                break;
            default:
                break;
        }
    }

    private void setDialog() {
        if (powerSet.size() != 0) {
            PromptButton[] buttons = new PromptButton[powerSet.size() + 1];
            buttons[0] = new PromptButton("取消", null);
            int i = 0;
            for (String s : powerSet) {
                i++;
                buttons[i] = new PromptButton(s, this);
            }
            promptDialog.showBottomAlert("", true, buttons);
        }
    }

    @Override
    public void onClick(PromptButton button) {
        switch (button.getText()) {
            case POWER_QUEREN:
                if ("1".equals(companyType) && "1".equals(outsourceFlag)) {  // 业主 内协单
                    ComplainPersonActivity.OpenActivity(this, newServerRequestId);
                } else {
                    ComplainPersonActivity.OpenActivity(this, newRequestId);
                }

                break;
            case POWER_CHULI:
                intent = new Intent(this, ComplaintsFeedbackActivity.class);
                intent.putExtra("classification", "建议");
                intent.putExtra("nowState", bean.getRows().getNowStat());
                if ("1".equals(companyType) && "1".equals(outsourceFlag)) {  // 业主 内协单
                    intent.putExtra("newRequestId", newServerRequestId);
                } else {
                    intent.putExtra("newRequestId", newRequestId);
                }
                intent.putExtra("complainId", complainId);
                startActivity(intent);
                break;
            case POWER_ZHIPAIGEI:
                ProblemAssignActivity.OpenActivity(this, "投诉指派", complainId, newRequestId);
                break;
            case POWER_FANKUI:
                intent = new Intent(this, ComplaintsFeedbackActivity.class);
                intent.putExtra("classification", "反馈");
                intent.putExtra("nowState", bean.getRows().getNowStat());
                if ("1".equals(companyType) && "1".equals(outsourceFlag)) {  // 业主 内协单
                    intent.putExtra("newRequestId", newServerRequestId);
                } else {
                    intent.putExtra("newRequestId", newRequestId);
                }
                intent.putExtra("complainId", complainId);
                intent.putExtra("sendUserName", sendUserName);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    public static void OpenActivity(Context context, String requestId, String newRequestId,
                                    String serverRequestId, String newServerRequestId,
                                    String outsourceFlag, String complainId, String sendUserName) {
        Intent intent = new Intent(context, ComplaintsInformationActivity.class);
        if (context instanceof Application) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra("requestId", requestId);
        intent.putExtra("newRequestId", newRequestId);
        intent.putExtra("serverRequestId", serverRequestId);
        intent.putExtra("newServerRequestId", newServerRequestId);
        intent.putExtra("outsourceFlag", outsourceFlag);
        intent.putExtra("complainId", complainId);
        intent.putExtra("sendUserName", sendUserName);
        context.startActivity(intent);
    }
}
